// 时间转换
function renderTime(date) {
  let dateee = new Date(date).toJSON();
  return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '') 
};

// --------------获取评论内容-------------
function commentsList() {
  $.ajax({
    type: 'get',
    url: '/admin/comments',
    
    success: function (res) {
      if (res.status === 0) {
        console.log(res);
        let str = '';
        res.data.forEach(item => {
          str += `
          <tr>
        <td>${item.id}</td>
        <td>${item.uname}</td>
        <td>${item.content}</td>
        <td>${renderTime(item.cdate)}</td>
        <td>
          <button data-id="${item.id}" type="button" class="layui-btn layui-btn-xs layui-btn-danger delete">
            删除
          </button>
        </td>
      </tr>
          `;
        });
        $('tbody').html(str);
      }
    }
  })
};
commentsList();







// ------------------删除功能------------
$('.layui-table tbody').on('click', '.delete', function () {
  // 获取id
  let id = $(this).data('id');
  // console.log(id);
  layer.confirm('你确定要删除吗', function (index) {
    $.ajax({
      type: 'DELETE',
      url: '/admin/comments/'+id,
     
      success: function (res) {
        if (res.status === 0) {
          console.log(1);
          layer.msg(res.message);
          commentsList();
        }
      }
    });
    // 关闭弹层
    layer.close(index);
  })
})